package com.esun.payment_api.controller;

import com.esun.common.api.CommonResult;
import com.esun.common.utils.BlankUtils;
import com.esun.payment_api.domain.Company;
import com.esun.payment_api.domain.Department;
import com.esun.payment_api.domain.User;
import com.esun.payment_api.domain.UserDept;
import com.esun.payment_api.security.model.ChangePasswordBody;
import com.esun.payment_api.service.LogService;
import com.esun.payment_api.service.UserService;
import com.esun.payment_api.security.model.RegisterBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    LogService logService;

    final String moduleName = "用户模块";

    //单个添加用户
    @PostMapping("/addUser")
    public CommonResult addUser(@RequestBody User user){
        CommonResult commonResult;
        if(user.getLoginName()==null||user.getPassword()==null||user.getUserName()==null||user.getCoId()==null){
            return CommonResult.failed("登录名、密码、用户名或单位ID为空");
        }
        //未启用-设置默认密码。启用方法：去除上方密码为空的判断
        if(user.getPassword()==null){
            user.setPassword("123456");
        }
        Date date = new Date();
        user.setPwdSetAt(date);
        try{
            //数据库中增加此用户（以及判断是否可以添加此用户，比如账号限制）
            user = userService.addUser(user);
            //数据库中增加该用户的用户-角色表的记录
            userService.setUserRole(user);
            commonResult = CommonResult.success(null, "添加用户成功，用户id为" + user.getId());
        } catch (Exception e) {
            commonResult = CommonResult.failed("添加用户失败");
            logService.writeErrorLog("添加用户失败，参考原因：" + e.getMessage(),moduleName);
        }
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    //批量添加用户（测试）
    @PostMapping("/batchAddUser")
    public CommonResult batchAddUser(List<User> userList){
        try{
            for (User user : userList){
                addUser(user);
            }
        } catch (Exception e){
            logService.writeErrorLog("批量添加用户失败,参考原因"+e.getMessage(),moduleName);
            return CommonResult.failed("批量添加用户失败");
        }
        return CommonResult.success(null, "批量添加用户成功");
    }

    //修改用户信息
    @PostMapping("/editUser")
    public CommonResult editUser(@RequestBody User user){
        if(user.getId()==null){
            return CommonResult.failed("请把需要修改的用户的id传过来");
        }
        CommonResult commonResult = userService.editUser(user);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    //修改用户密码
    @PostMapping("/changePassword")
    public CommonResult changePassword(@RequestBody ChangePasswordBody changePasswordBody){
        if(changePasswordBody.getId()==null||changePasswordBody.getOldPassword()==null||changePasswordBody.getNewPassword()==null){
            return CommonResult.failed("用户id、旧密码或新密码没有收到");
        }
        CommonResult commonResult = userService.changePassword(changePasswordBody);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    //软删用户
    @PostMapping("/deleteUser")
    public CommonResult deleteUser(@RequestBody User user){
        if(user.getId()==null){
            return CommonResult.failed("请把需要删除的用户的id传过来");
        }
        user.setIsDeleted("1");
        CommonResult commonResult = userService.editUser(user);
        String message = commonResult.getMessage();
        message = message.replaceAll("更新", "删除");
        commonResult.setMessage(message);
        logService.writeOperationLog(moduleName, commonResult);
        return commonResult;
    }

    //查询职务列表
    @GetMapping("/getRoleList")
    public CommonResult getRoleList(Short companyId){
        if(companyId==null){
            return CommonResult.failed("公司ID不能为空");
        }
        return userService.getRoleList(companyId);
    }

    //查询用户列表
    @GetMapping("/getUserList")
    public CommonResult getUserList(Short companyId, Short departmentId){
        if(companyId==null){
            return CommonResult.failed("公司ID不得为空");
        }
        CommonResult commonResult = userService.getUserList(companyId, departmentId);
        logService.writeAccessLog(moduleName, commonResult);
        return commonResult;
    }

    //查询单位字典
    @GetMapping("/getCompanyList")
    public CommonResult getCompanyList(){
        return userService.getCompanyList();
    }

    //查询部门字典
    @GetMapping("/getDepartmentList")
    public CommonResult getDepartmentList(Short companyId){
        return userService.getDepartmentList(companyId);
    }

    //查询可访问的菜单列表
    @GetMapping("/getMenuList")
    public CommonResult getMenuList(Short userId){
        if(userId==null){
            return CommonResult.failed("用户id不得为空");
        }
        return userService.getMenuList(userId);
    }

    @GetMapping("/getUserDeptById")
    public CommonResult getUserDeptById(@RequestParam Short userId){
        if(BlankUtils.isBlank(userId)){
            return CommonResult.failed("用户id不得为空");
        }
        UserDept userDept = userService.getUserDeptById(userId);
        return CommonResult.success(userDept, "获取用户部门信息成功");
    }

}